package com.whatever;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class CalculatorActivity extends Activity {
	
	public static Integer value1;
	public static Integer value2;
	public static Integer answer;
	public static int j;
	public static int k;
	public static int[] ids = {R.id.button0, 
		R.id.button1,
		R.id.button2, 
		R.id.button3, 
		R.id.button4, 
		R.id.button5, 
		R.id.button6, 
		R.id.button7, 
		R.id.button8, 
		R.id.button9, 
		R.id.button10,
		R.id.button11, 
		R.id.button12, 
		R.id.button13, 
		R.id.button14, 
	};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        value1 = null;
        value2 = null;
        j = 0;
        int numButtons = 15;
        
        EditText text = (EditText)findViewById(editText1);
        Button[] button = new Button[numButtons];
        
        for(int i = 0; i < numButtons; i++) { //For every button, initialize it using a key of ids
        	button[i] = (Button)findViewById(ids[i]);
        }
        
        for(int i = 0; i < 9; i++) {
        	j = i;
        	button[i].setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					if(value1 == null) {
						value1 = j;
					}else if(value2 == null) {
						value2 = j;
					}
				}
			});
        }
        
        if(value1 != null && value2 != null) {
        	for(int i = 11; i < 15; i++) {
        		k = i;
        		button[i].setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						answer = calculate(k);
					}
				});
        	}
        	
        	button[10].setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					output.setText(answer.toString());
				}
			});
        	
        	
        	
        	
        	
        	
        	
        }
    }
    
    public static int calculate(int a) {
    	Integer result = null;
    	if(a == 11)
    		result = value1 + value2;
    	if(a == 12)
    		result = value1 - value2;
    	if(a == 13)
    		result = value1 * value2;
    	if(a == 14)
    		result = value1 / value2;
    	return result;
    }
}